package com.hckjsec.official.controller;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.constant.HttpStatus;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.core.page.TableDataInfo;
import com.hckjsec.common.utils.ServletUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.ip.IpUtils;
import com.hckjsec.official.domain.*;
import com.hckjsec.official.domain.VisitView.OfficialCaseView;
import com.hckjsec.official.domain.VisitView.OfficialGlobalView;
import com.hckjsec.official.domain.VisitView.OfficialPublicCaseView;
import com.hckjsec.official.domain.VisitView.OfficialView;
import com.hckjsec.official.service.*;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 此类处理公司网站请求的接口数据
 */
@RestController
@RequestMapping("/official/api")
public class OfficialPublicApiController extends BaseController {

    @Autowired
    private IOfficialConfigService officialConfigService;

    @Autowired
    private IOfficialProductService officialProductService;

    @Autowired
    private IOfficialHonorService officialHonorService;

    @Autowired
    private IOfficialNewsService officialNewsService;

    @Autowired
    private IOfficialCaseService officialCaseService;

    @Autowired
    private IOfficialRecruitService officialRecruitService;

    @Autowired
    private OfficialLoginLogService officialLoginLogService;
    /**
     * 公司网站初始化
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/global")
    public AjaxResult global(@RequestBody OfficialView officialView) {
        OfficialGlobalView  globalView = new OfficialGlobalView();
        String appid = StringUtils.isHcNull(officialView.getAppid(), "");
        if ("".equals(appid)) {
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialConfig officialConfig = officialConfigService.selectOfficialConfigById(appid);
        if (StringUtils.isNull(officialConfig)){
            return AjaxResult.cloud_602("无访问权限");
        }
        globalView.setConfig(officialConfig);
        OfficialProduct officialProduct = new OfficialProduct();
        officialProduct.setState("true");
        officialProduct.setWhetherHome("true");
        List<OfficialProduct> productList = officialProductService.selectOfficialProductList(officialProduct);
        productList.forEach(kv->{
            kv.setContent(null);
        });
        productList.sort(Comparator.comparing(OfficialProduct::getSort));
        globalView.setProductList(productList);
        OfficialHonor officialHonor = new  OfficialHonor();
        officialHonor.setType("1");
        officialHonor.setState("true");
        List<OfficialHonor> honorList = officialHonorService.selectOfficialHonorList(officialHonor);
        honorList.sort(Comparator.comparing(OfficialHonor::getSort));
        globalView.setHonor(honorList);
        //记录日志
        officialConfig.setVisits(officialConfig.getVisits()+1);
        officialConfigService.updateOfficialConfig(officialConfig);
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        officialLoginLogService.sendWebOfficialLog(userAgent,ip);
        return AjaxResult.success(globalView);
    }

    /**
     * 公司网站产品服务
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getProduct")
    public AjaxResult getProduct(@RequestBody OfficialView officialView) {
        String appid = StringUtils.isHcNull(officialView.getAppid(), "");
        if ("".equals(appid)) {
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialProduct officialProduct = new OfficialProduct();
        officialProduct.setState("true");
        List<OfficialProduct> productList = officialProductService.selectOfficialProductList(officialProduct);
        productList.forEach(kv->{
            kv.setContent(null);
        });
        productList.sort(Comparator.comparing(OfficialProduct::getSort));
        return AjaxResult.success(productList);
    }

    /**
     * 公司网站产品服务详情
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getProductDetail")
    public AjaxResult getProductDetail(@RequestBody OfficialView officialView) {
        Long productId = officialView.getProductId();
        if(StringUtils.isNull(productId)){
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialProduct officialProduct = officialProductService.selectOfficialProductById(productId);
        return AjaxResult.success(officialProduct);
    }

    /**
     * 公司网站新闻动态
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getNews")
    public TableDataInfo getNews(@RequestBody OfficialView officialView)
    {
        String appid = StringUtils.isHcNull(officialView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("id不能为空");
        }
        startWebPage(officialView.getPageNum(),officialView.getPageSize());
        OfficialNews officialNews = new OfficialNews();
        officialNews.setState("true");
        List<OfficialNews> list = officialNewsService.selectOfficialNewsWebList(officialNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 公司网站新闻动态详情
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getNewsDetail")
    public AjaxResult getNewsDetail(@RequestBody OfficialView officialView) {
        Long newsId = officialView.getNewsId();
        if(StringUtils.isNull(newsId)){
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialNews officialNews = officialNewsService.selectOfficialNewsById(newsId);
        return AjaxResult.success(officialNews);
    }

    /**
     * 公司网站案例
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getCase")
    public AjaxResult getCase(@RequestBody OfficialView officialView) {
        List<OfficialCaseView> list = new ArrayList<>();
        String appid = StringUtils.isHcNull(officialView.getAppid(), "");
        if ("".equals(appid)) {
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialCase officialCase = new OfficialCase();
        officialCase.setState("true");
        List<OfficialCase> officialCaseList = officialCaseService.selectOfficialCaseOrderList(officialCase);
        officialCaseList.forEach(kv->{
            String code = kv.getCode();
            OfficialCaseView officialCaseView = new  OfficialCaseView();
            officialCaseView.setName(kv.getAliasName());
            officialCaseView.setAlias(kv.getCodeName());
            officialCaseView.setValue(kv.getDeptId());
            OfficialCase findOfficialCase= new OfficialCase();
            findOfficialCase.setState("true");
            findOfficialCase.setCode(code);
            List<OfficialCase> findOfficialCaseList = officialCaseService.selectOfficialCaseList(findOfficialCase);
            List<OfficialPublicCaseView> tableData = new ArrayList<>();
            findOfficialCaseList.forEach(op->{
                OfficialPublicCaseView officialPublicCaseView = new OfficialPublicCaseView();
                officialPublicCaseView.setId(op.getId());
                officialPublicCaseView.setTitle(op.getTitle());
                officialPublicCaseView.setTime(op.getReleaseTime());
                tableData.add(officialPublicCaseView);
            });
            officialCaseView.setTableData(tableData);
            list.add(officialCaseView);
        });
        return AjaxResult.success(list);
    }


    /**
     * 公司网站案例详情
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getCaseDetail")
    public AjaxResult getCaseDetail(@RequestBody OfficialView officialView) {
        Long caseId = officialView.getCaseId();
        if(StringUtils.isNull(caseId)){
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialCase officialCase = officialCaseService.selectOfficialCaseById(caseId);
        return AjaxResult.success(officialCase);
    }

    /**
     * 公司网站公司招聘
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getRecruit")
    public AjaxResult getRecruit(@RequestBody OfficialView officialView) {
        String appid = StringUtils.isHcNull(officialView.getAppid(), "");
        if ("".equals(appid)) {
            return AjaxResult.cloud_602("id不能为空");
        }
        OfficialRecruit officialRecruit = new OfficialRecruit();
        officialRecruit.setState("true");
        List<OfficialRecruit> list = officialRecruitService.selectOfficialRecruitList(officialRecruit);
        return AjaxResult.success(list);
    }

    protected TableDataInfo getDataErrorTable(String msg)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.HTTP_602);
        rspData.setMsg(msg);
        rspData.setRows(null);
        rspData.setTotal(0);
        return rspData;
    }
}
